package com.coda.service.impl;

import com.coda.adminVo.agreement.AgreementQueryVo;
import com.coda.adminVo.agreement.AgreementVo;
import com.coda.constant.MessageConstant;
import com.coda.entity.*;
import com.coda.exception.LeaseAgreementException;
import com.coda.mapper.*;
import com.coda.result.PageResult;
import com.coda.result.Result;
import com.coda.service.LeaseAgreementService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
@Slf4j
@Service
public class LeaseAgreementServiceImpl implements LeaseAgreementService {
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentMapper apartmentMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    /**
     * 根据条件分页查询租约列表
     *
     * @return
     */
    @Override
    public PageResult getPageList(@RequestParam Integer current, @RequestParam Integer size, AgreementQueryVo queryVo) {
        //设置分页参数
        PageHelper.startPage(current, size);
        Page<AgreementVo> agreementVos = leaseAgreementMapper.getPageList(queryVo);
        PageResult pageResult = new PageResult(agreementVos.getTotal(), agreementVos.getResult());
        return pageResult;
    }
    /**
     * 根据id查询租约信息
     * @param id
     * @return
     */
    @Override
    public AgreementVo getById(Long id) {
        //查询租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.getViewAppointement(id);
        //查询公寓信息
        ApartmentInfo apartment = apartmentMapper.getApartment(leaseAgreement.getApartmentId());
        //查询房间信息
        RoomInfo room = roomMapper.getRoom(leaseAgreement.getRoomId());
        //查询支付方式信息
        PaymentType paymentType = paymentTypeMapper.getPaymentType(leaseAgreement.getPaymentTypeId());
        //查询租期
        LeaseTerm leaseTerm = leaseTermMapper.getLeaseTerm(leaseAgreement.getLeaseTermId());

        AgreementVo agreementVo = new AgreementVo();
        BeanUtils.copyProperties(leaseAgreement,agreementVo);
        agreementVo.setApartmentInfo(apartment);
        agreementVo.setRoomInfo(room);
        agreementVo.setPaymentType(paymentType);
        agreementVo.setLeaseTerm(leaseTerm);
        return agreementVo;
    }

    /**
     * 保存或更新租约信息
     * @param leaseAgreement
     */
    @Override
    public void saveOrUpdate(LeaseAgreement leaseAgreement) {
        //判断是否有id，有：update，没有：insert
        if (leaseAgreement.getId() == null ){
            int count = leaseAgreementMapper.getCount(leaseAgreement);
            if (count>0){
                throw new LeaseAgreementException(MessageConstant.ROOM_IS_STAY);
            }
            leaseAgreementMapper.insert(leaseAgreement);
        }else {
           //如果跟新到期时间大于当前时间就设置状态为已签约
            if (leaseAgreement.getLeaseEndDate().isAfter(LocalDate.now())){
                leaseAgreement.setStatus(2);
                leaseAgreementMapper.update(leaseAgreement);
            }else {
                leaseAgreement.setStatus(4);
                leaseAgreementMapper.update(leaseAgreement);
            }

        }
    }

    /**
     * 根据ID删除租约信息
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        leaseAgreementMapper.deleteById(id);
    }

    /**
     * 根据ID更新租约状态
     * @param id
     */
    @Override
    public void updateStatusById(Long id,Integer status) {
        LeaseAgreement leaseAgreement = new LeaseAgreement();
        leaseAgreement.setId(id);
        leaseAgreement.setStatus(status);
        leaseAgreementMapper.updateStatus(leaseAgreement);
    }


}
